package com.idudiao.sc.neo4j.controller;

import com.idudiao.sc.neo4j.model.node.Customer;
import com.idudiao.sc.neo4j.model.relationship.Transaction;
import com.idudiao.sc.neo4j.repository.CustomerRepository;
import com.idudiao.sc.neo4j.repository.TranscationRepository;
import com.idudiao.sc.neo4j.service.Neo4jService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.neo4j.ogm.model.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author songyinyin
 * @since 2019/1/7 15:49
 */
@Controller
@Slf4j
@AllArgsConstructor
@Api(tags = "neo4j测试")
public class TransactionController {

    private final CustomerRepository customerRepository;
    private final TranscationRepository transcationRepository;
    private final Neo4jService neo4jService;

    @ApiOperation("初始化节点和关系")
    @RequestMapping(value = "/bot/init", method = RequestMethod.GET)
    public void init() {
        customerRepository.deleteAll();
        transcationRepository.deleteAll();

        Customer xiaoai = new Customer("小爱");
        Customer hanbao = new Customer("汉宝");
        Customer dudiao = new Customer("读钓");
        Customer zhiyong = new Customer("志勇");

        Transaction t1 = transcationRepository.save(new Transaction(xiaoai, hanbao, 100));
        Transaction t2 = transcationRepository.save(new Transaction(hanbao, dudiao, 150));
        Transaction t3 = transcationRepository.save(new Transaction(dudiao, zhiyong, 600));
        Transaction t4 = transcationRepository.save(new Transaction(hanbao, zhiyong, 132));
        Transaction t5 = transcationRepository.save(new Transaction(zhiyong, xiaoai, 99));
        Transaction t6 = transcationRepository.save(new Transaction(dudiao, hanbao, 963));
    }

    @ApiOperation("添加节点和关系")
    @RequestMapping(value = "/bot/add", method = RequestMethod.POST)
    public void add(@RequestBody Transaction transaction) {
        transcationRepository.save(transaction);
    }

    @ApiOperation("删除节点和关系")
    @RequestMapping(value = "/bot/delete", method = RequestMethod.GET)
    public void delete() {
        customerRepository.deleteAll();
        transcationRepository.deleteAll();
    }

    @ApiOperation("删除节点（会关联删除所有关系）")
    @RequestMapping(value = "/bot/delete/node", method = RequestMethod.GET)
    public void deleteNode(Long id) {
        customerRepository.deleteById(id);
    }

    @ApiOperation("删除标签")
    @RequestMapping(value = "/bot/delete/label", method = RequestMethod.GET)
    public void deleteLabel(String label) {

        neo4jService.deleteLabel(label);
    }

    @ApiOperation("删除关系")
    @RequestMapping(value = "/bot/delete/relation", method = RequestMethod.GET)
    public void deleteRelation(Long id) {
        transcationRepository.deleteById(id);
    }

    @ApiOperation("获取节点")
    @RequestMapping(value = "/bot/get/node", method = RequestMethod.GET)
    @ResponseBody
    public Object getNode() {
        Iterable<Customer> all = customerRepository.findAll();
        return all;
    }

    @ApiOperation("获取关系")
    @RequestMapping(value = "/bot/get/relation", method = RequestMethod.GET)
    @ResponseBody
    public Object getRelation(String name, int depth) {
        Iterable<Transaction> all = transcationRepository.findByStart_Name(name, depth);

        return all;
    }

    @ApiOperation("加载数据-loadTest")
    @RequestMapping(value = "/load/test", method = RequestMethod.GET)
    @ResponseBody
    public Object loadTest() {

        Result load = transcationRepository.loadTest("http://data.neo4j.com/northwind/products.csv");

        return load;
    }

    @ApiOperation("加载数据-Transcation")
    @RequestMapping(value = "/load/transcation", method = RequestMethod.GET)
    @ResponseBody
    public Object loadTranscation(String url, String label) {

        if (!url.startsWith("http")) {
            url = "file:///" + url;
        }
        Result load = transcationRepository.loadTranscation(url, label);

        return load;
    }

}
